Skip to main content

Bit Manipulation LeetCode Questions

  1. Number of 1 Bits - Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight).

  2. Power of Two - Given an integer, write a function to determine if it is a power of two.

  3. Counting Bits - Given a non negative integer number num, for every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array.

  4. Reverse Bits - Reverse bits of a given 32 bits unsigned integer.

  5. Single Number - Given a non-empty array of integers, every element appears twice except for one. Find that single one.

  6. Single Number II - Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one.

  7. Single Number III - Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.

  8. Sum of Two Integers - Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.

  9. Missing Number - Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.

  10. Repeated DNA Sequences - All DNA is composed of a series of nucleotides abbreviated as 'A', 'C', 'G', and 'T', finds all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule.

  11. Bitwise AND of Numbers Range - Given a range [m, n], return the bitwise AND of all numbers in this range, inclusive.

  12. UTF-8 Validation - A character in UTF8 can be from 1 to 4 bytes long. Given an array of integers representing the data, return whether it is a valid utf-8 encoding.

  13. Total Hamming Distance - The Hamming distance between two integers is the number of positions at which the corresponding bits are different.

  14. Binary Watch - A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59).

  15. Maximum XOR of Two Numbers in an Array - Given an array of numbers, find the maximum result of ai XOR aj, where 0 ≤ i, j < n.

  16. Find the Difference - Given two strings s and t which consist of only lowercase letters. String t is generated by random shuffling string s and then add one more letter at a random position. Find the letter that was added in t.

  17. Binary Number with Alternating Bits - Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values.

  18. Complement of Base 10 Integer - Every non-negative integer N has a binary representation. The complement of a number is the number you get when you flip every bit in its binary representation.

  19. Binary Gap - Given a positive integer N, find and return the longest distance between two consecutive 1's in the binary representation of N.

  20. Convert Binary Number in a Linked List to Integer - Given head which is a reference node to a singly-linked list. The value of each node in the linked list is either 0 or 1.

  21. XOR Operation in an Array - Given an integer n and an integer start, define an array nums where nums[i] = start + 2*i (0-indexed) and n == nums.length. Return the bitwise XOR of all elements of nums.

  22. Find XOR Sum of All Pairs Bitwise AND - Given two arrays nums1 and nums2, find the XOR sum of all pairs bitwise AND.

  23. Sum of All Subset XOR Totals - The XOR total of an array is defined as the bitwise XOR of all its elements.

  24. Minimum XOR Sum of Two Arrays - You are given two integer arrays nums1 and nums2 of length n. Return the minimum possible XOR sum of pairs of integers.

  25. Check If a String Contains All Binary Codes of Size K - Given a binary string s and an integer k, return true if every binary code of length k is a substring of s.

  26. Decode XORed Array - Given an encoded array where encoded[i] = arr[i] XOR arr[i + 1], return the original array arr.

  27. Minimum One Bit Operations to Make Integers Zero - Given an integer n, you must transform it into 0 using the minimum number of one bit operations you can perform.

  28. Maximum XOR for Each Query - You are given a sorted array and an integer, return the maximum XOR value for each query.

  29. Maximum Product of Splitted Binary Tree - Given a binary tree, split the binary tree into two subtrees by removing one edge such that the product of the sums of the subtrees is maximized.

  30. Maximum Good People Based on Statements - Given a list of statements about people, return the maximum number of good people based on the statements.